#include "recovery/fs/file/FileAppender.h"

#include "recovery/fs/file/File.h"
namespace recovery {
namespace fs {
namespace file {

ErrnoStatus FileAppender::openExist(const std::string & path, std::unique_ptr<FileAppender> & fa){
    fa.reset(new FileAppender(path));
    return fa->file().openForAppend(fa->writableFd());
}
ErrnoStatus FileAppender::create(const std::string & path, std::unique_ptr<FileAppender> &fa){
    fa.reset(new FileAppender(path));
    return fa->file().createForAppend(fa->writableFd());
}

FileAppender::FileAppender(const std::string & path)
: AbstractFileOperation(path, true)
{
}

ErrnoStatus FileAppender::append(const char * buf, const size_t len, size_t & appendedBytes){
    return file().append(fd(),  reinterpret_cast<const uint8_t*>(buf), len, appendedBytes);
}
ErrnoStatus FileAppender::append(const std::string & data, size_t & appendedBytes){
    return file().append(fd(), reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), appendedBytes);
}
ErrnoStatus FileAppender::sync(){
    return file().sync(fd());
}
ErrnoStatus FileAppender::closeOnce(){
    return file().close(fd());
}

} //end of namespace file
} //end of namespace fs
} //end of namespace recovery

